package routers

import (
	"Suscan/pkg/utils"
	"Suscan/routers/api"
	"github.com/gin-gonic/gin"
	"net/http"
)

func InitRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(Cors())


	//登陆接口
	r.POST("/api/login", api.Login)
	r.GET("/api/currentUser", api.CurrentUser)

	//端口扫描
	//r.GET("/startscan", api.StartScan)
	//关闭端口扫描
	//r.GET("/stopscan", api.StoptScan)

	//r.GET("/getVulPort", api.Getvulport)


	apiv1 := r.Group("/api")
	apiv1.Use(utils.JWT())
	{
		//查看所有资产
		apiv1.GET("/assets", api.GetAssets)
		//设置：添加资产
		apiv1.POST("/assets", api.AssetsSetup)
		//设置：删除资产
		apiv1.DELETE("/assets", api.DeleteAsset)

		//查看域名转换结果
		apiv1.GET("/url", api.GetUrl)
		//设置：删除资产
		apiv1.DELETE("/url", api.DeleteUrl)

		//查看端口扫描结果
		apiv1.GET("/iplist", api.GetIplist)
		//设置：删除端口扫描结果
		apiv1.DELETE("/iplist", api.DeleteIplist)


		//端口扫描
		apiv1.GET("/startscan", api.StartScan)
		//关闭端口扫描
		apiv1.GET("/stopscan", api.StoptScan)
		//端口扫描状态
		apiv1.GET("/scanstatus", api.ScanStatus)


		////修改扫描设置
		//apiv1.POST("/scansetting", api.ScanSetup)

		//获取高危端口与协议结果
		apiv1.GET("/getVulPort", api.Getvulport)
	}


	return r
}

func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization,Token,X-TOKEN ")
		c.Header("Access-Control-Allow-Methods", "POST, GET,PUT, DELETE, OPTIONS")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")

		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		// 处理请求
		c.Next()
	}
}
